﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StatisticWord.Question;

namespace StatisticWord.Answer
{
    class Jimson : IStatisticWord
    {
        public string IStatisticWord(string str)
        {
            string res =  string.Empty;

          //   Dictionary<string, int> WordDic = new Dictionary<string, int>();
            List<Word> WordDic = new List<Word>();
            string build = "";
            bool hasword = false;
            foreach (char item in (str + " ").ToCharArray())
            {
                if (Char.IsLetterOrDigit(item))
                {
                    build += item;
                    hasword = true;
                }
                else
                {
                    if (hasword)
                    {
                        var sstr = WordDic.Where(s => s.str.ToLower() == build.ToLower()).FirstOrDefault();
                        if (sstr != null)
                        {
                            sstr.val++;
                        }
                        else
                        {
                            WordDic.Add(new Word{str=build, val = 1});
                        }
                        build = "";
                        hasword = false;
                    }
                }
            }
            foreach (var item in WordDic)
            {
                res += item.str + ":" + item.val.ToString() + ";";
            }
            return res;
        
        }
    }

    class Word
    {
        public int val { get; set; }
        public string str { get; set; }
    }
}
